const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ['js', 'css']
const packageJson = require('./package.json');
const webpack = require("webpack");
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

module.exports = {
    assetsDir: 'static',

    outputDir: 'dist/' + packageJson.name,

	productionSourceMap: process.env.NODE_ENV === 'development',

    configureWebpack: {
        devtool: '#cheap-source-map',
    },

    chainWebpack: config => {
        config.when(+process.env.VUE_APP_ANALYZER === 1, function () {
            config.plugin('webpack-bundle-analyzer').use(BundleAnalyzerPlugin);
        });

        // config.when(process.env.NODE_ENV === 'production', function () {
	    //     const UglifyPlugin = require('uglifyjs-webpack-plugin');
        // });

        //代码压缩
        config.plugin('gzip').use(
            new CompressionWebpackPlugin({
                algorithm: 'gzip',
                test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
                threshold: 10240,
                minRatio: 0.8
            })
        );

        //去除moment语言包
        config.plugin('ignore').use(
            new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, /zh-cn$/)
        );

        // 修复HMR
        config.resolve.symlinks(true);

        //单独打包vant
        config.optimization.splitChunks({
            cacheGroups: {
                vant: {
                    name: 'chunk-vant',
                    test: /[\\/]node_modules[\\/]vant[\\/]/,
                    chunks: 'all',
                    priority: 3,
                    reuseExistingChunk: true,
                    enforce: true
                },
            }
        });

        return config;
    },

    devServer: {
        // open: false,
        // port: 8801, // 自定义修改8080端口
        // 代理跨域
        proxy: {
            "/__api/*": {
                target: '{SERVER_ADDRESS}',
                pathRewrite: {
                    "/__api": ""
                }
            },
            "/__minio/*": {
                target: 'https://file.baicaidi.com/',
                pathRewrite: {
                    "/__minio": ""
                }
            }
        }
    }
};
